package com.king.demo.config;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.beans.Introspector;

public class CustomAnnotationBeanNameGenerator extends AnnotationBeanNameGenerator {

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        String beanClassName = definition.getBeanClassName();
        System.out.println("beanClassName-" + beanClassName);
        Assert.state(beanClassName != null, "No bean class name set");
        String shortClassName = ClassUtils.getShortName(beanClassName);
        if(definition.getBeanClassName().startsWith("com.king.demo.interfaces")){
            shortClassName = "ShopFront" + shortClassName;
        }else if(definition.getBeanClassName().startsWith("com.king.demo.impl")){
            shortClassName = shortClassName.substring(0, shortClassName.length()-4) + "Interface";
        }
        return Introspector.decapitalize(shortClassName);
    }
}
